﻿using EntityModel;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Mvc;

namespace API.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class WaresController : ControllerBase
    {
        private readonly BitDbContext _db;
        public WaresController(BitDbContext db)
        {
            _db = db;
        }
        /// <summary>
        /// 查看数据
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public Result GetWares()
        {
            Result result = new Result();
            List<EM_Wares> wares = _db.EM_Ware.ToList();
            if (wares != null)
            {
                result.Code = 200;
                result.Data = wares;
            }
            else
            {
                result.Code = 404;
            }
            result.Author = "";
            return result;
        }
        /// <summary>
        /// 添加数据
        /// </summary>
        /// <param name="ware"></param>
        /// <returns></returns>
        [HttpPost]
        public Result AddWare([FromBody] EM_Wares ware)
        {
            Result result = new Result();
            _db.EM_Ware.Add(ware);
            result.Data=_db.SaveChanges();
            return result;
        }
       /// <summary>
       /// 删除数据
       /// </summary>
       /// <param name="id"></param>
       /// <returns></returns>
        [HttpDelete]
        public Result DelUser(Guid id)
        {
            Result result = new Result();
            EM_Wares? user = _db.EM_Ware.Where(w => w.Id == id).SingleOrDefault();
            if (user != null)
                _db.EM_Ware.Remove(user);
            result.Data = _db.SaveChanges();
            return result;
        }
        /// <summary>
        /// 条件查询
        /// </summary>
        /// <returns></returns>
        [HttpGet("{Category}")]
        public Result GetWaresLike(string category)
        {
            Result result = new Result();
            result.Data = _db.EM_Ware.Where(w => w.Category == category).ToList();
            return result;
        }
        /// <summary>
        /// 修改数据
        /// </summary>
        /// <param name="em_wares"></param>
        /// <returns></returns>
        [HttpPut]
        public Result Put([FromBody] EM_Wares em_wares)
        {
            Result result = new Result();
            _db.EM_Ware.Update(em_wares);
            result.Data = _db.SaveChanges();
            return result;
        }
        /// <summary>
        /// 多条语句查询
        /// </summary>
        /// <param name="name"></param>
        /// <param name="category"></param>
        /// <returns></returns>
        //[HttpGet]
        //public IQueryable<EM_Wares> GetAll([FromHeader] string name, [FromHeader] string category)
        //{
        //    IQueryable<EM_Wares> query = _db.EM_Ware;
        //    if (!string.IsNullOrEmpty(name){
        //        query = query.Where(s => s.Name == name);
        //    }
        //    if (!string.IsNullOrEmpty(category){
        //        query = query.Where(s => s.Category == category);

        //    }
        //    return query;
        //}
    }
}
